package com.k.kc.util;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author langjw
 */
public class ZipUtils {
    public static void zip(String zipFileName,String sourceFileName) throws Exception {
        //File zipFile = new File(zipFileName);
        System.out.println("压缩中...");

        //创建zip输出流
        ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFileName));

        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);

        File sourceFile = new File(sourceFileName);

        //调用函数
        compress(out,bos,sourceFile,sourceFile.getName());

        bos.close();
        out.close();
        System.out.println("压缩完成");

    }

    public static void compress(ZipOutputStream out,BufferedOutputStream bos,File sourceFile,String base) throws Exception  {
        //如果路径为目录（文件夹）
        if(sourceFile.isDirectory()) {
            //取出文件夹中的文件（或子文件夹）
            File[] flist = sourceFile.listFiles();
            //如果文件夹为空，则只需在目的地zip文件中写入一个目录进入点
            if(flist.length==0) {
                System.out.println(base+"/");
                out.putNextEntry(  new ZipEntry(base+"/") );
            } else {
                //如果文件夹不为空，则递归调用compress，文件夹中的每一个文件（或文件夹）进行压缩
                for(int i=0;i<flist.length;i++) {
                    compress(out,bos,flist[i],base+"/"+flist[i].getName());
                }
            }
        }
        else {
            //如果不是目录（文件夹），即为文件，则先写入目录进入点，之后将文件写入zip文件中
            out.putNextEntry( new ZipEntry(base) );
            FileInputStream fos = new FileInputStream(sourceFile);
            BufferedInputStream bis = new BufferedInputStream(fos);
            int tag;
            System.out.println(base);
            //将源文件写入到zip文件中
            while((tag=bis.read())!=-1){
                bos.write(tag);
            }
            bis.close();
            fos.close();
        }
    }

    private static void stringToFileToZip(String content,String fileName,String zipName){
        //创建zip输出流
        try{
            //ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            FileOutputStream outputStream = new FileOutputStream(zipName);
            ZipOutputStream zip = new ZipOutputStream(outputStream);
            zip.putNextEntry(new ZipEntry("abc\\abc.java"));
            zip.write(content.getBytes(StandardCharsets.UTF_8));
            //byte[] b = outputStream.toByteArray();
            //IOUtils.write("abc", zip, "UTF-8");
            zip.flush();
            zip.finish();
            zip.closeEntry();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private static void fileToZip(String srcFile,String zipFile){
        try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
             FileInputStream fis = new FileInputStream(srcFile);){

            zip.putNextEntry(new ZipEntry("xx\\abc.java"));
            int temp = 0;
            while ((temp=fis.read())!=-1){
                zip.write(temp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        try {
//            System.out.println("start zip");
//            zip("C:\\test\\upd.zip","C:\\test\\upd");
//            System.out.println("end zip");
//            fileToZip("c:/test/TestFunctionController.java","c:/test/ljw.zip");
            stringToFileToZip("hehe","abc.java","c:/test/abc11234.zip");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
